<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Seos;
use Illuminate\Http\Request;

class SeoController extends Controller {

    public function index(){

        $lists = Seos::orderBy('created_at', 'asc')
            ->paginate(20);
        return $this->writeJson(0,$lists);
    }

    public function update(Request $request)
    {
        $data = $request->all();
        if($data['id']){
            $info = Seos::where('id','<>',$data['id'])->where('name',$data['name'])->first();
            if($info){
                return $this->writeJson(1,null,'该SEO标识已经存在');
            }
            $info = Seos::findOrFail($data['id']);
            $info->update($data);
            return $this->writeJson(0,null,'修改SEO信息成功');
        }else{
            unset($data['id']);
            $info = Seos::where('name',$data['name'])->first();
            if($info){
                return $this->writeJson(1,null,'该SEO标识已经存在');
            }
            Seos::create($data);
            return $this->writeJson(0,null,'新增SEO信息成功');
        }
    }


    public function destroy(Request $request){
        $id = (int)$request->get('id',0);
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        Seos::destroy($id);
        return $this->writeJson(0);
    }

}
